package cst.a806yf.zyfypt_806yf.Activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import cst.a806yf.zyfypt_806yf.Fragment.Fragment1;
import cst.a806yf.zyfypt_806yf.Fragment.Fragment2;
import cst.a806yf.zyfypt_806yf.Fragment.Fragment3;
import cst.a806yf.zyfypt_806yf.Fragment.Fragment4;
import cst.a806yf.zyfypt_806yf.Fragment.Fragment5;
import cst.a806yf.zyfypt_806yf.R;


public class MainActivity extends AppCompatActivity {

    private  LinearLayout layout1,layout2,layout3,layout4,layout5;
    private  ImageButton ib1,ib2,ib3,ib4,ib5;
    private  ViewPager vp;
    List<Fragment> list;
    FragmentPagerAdapter fragmentPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show();

            }
        });


            init();
            initFragment();
    }

      private void initFragment(){
             list=new ArrayList<Fragment>();
             list.add(new Fragment1());
             list.add(new Fragment2());
             list.add(new Fragment3());
             list.add(new Fragment4());
             list.add(new Fragment5());

              fragmentPagerAdapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
                  @Override
                  public Fragment getItem(int position) {
                      return list.get(position);
                  }
                  @Override
                  public int getCount() {
                      return list.size();
                  }
              };
            vp=(ViewPager)findViewById(R.id.viewPager);
            vp.setAdapter(fragmentPagerAdapter);

             vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                 @Override
                 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                 }

                 @Override
                 public void onPageSelected(int position) {
                        vp.setCurrentItem(position);
                        resetImage();
                        selectTab(position);
                 }

                 @Override
                 public void onPageScrollStateChanged(int state) {

                 }
             });
      }


         View.OnClickListener OnClickListener= new View.OnClickListener() {
             @Override
          public void onClick(View v) {
                resetImage();
                 switch(v.getId()){
                     case R.id.layout1:
                         selectTab(0);
                         break;
                     case R.id.layout2:
                         selectTab(1);
                         break;
                     case R.id.layout3:
                         selectTab(2);
                         break;
                     case R.id.layout4:
                         selectTab(3);
                         break;
                     case R.id.layout5:
                         selectTab(4);
                         break;
                 }
           }
         };

        private void init(){
            ib1=(ImageButton)findViewById(R.id.imageButton1);
            ib2=(ImageButton)findViewById(R.id.imageButton2);
            ib3=(ImageButton)findViewById(R.id.imageButton3);
            ib4=(ImageButton)findViewById(R.id.imageButton4);
            ib5=(ImageButton)findViewById(R.id.imageButton5);

            layout1=(LinearLayout)findViewById(R.id.layout1);
            layout2=(LinearLayout)findViewById(R.id.layout2);
            layout3=(LinearLayout)findViewById(R.id.layout3);
            layout4=(LinearLayout)findViewById(R.id.layout4);
            layout5=(LinearLayout)findViewById(R.id.layout5);

            layout1.setOnClickListener(OnClickListener);
            layout2.setOnClickListener(OnClickListener);
            layout3.setOnClickListener(OnClickListener);
            layout4.setOnClickListener(OnClickListener);
            layout5.setOnClickListener(OnClickListener);
        }

      private void resetImage(){
          ib1.setImageResource(R.drawable.article_unpressed);
          ib2.setImageResource(R.drawable.tware_un);
          ib3.setImageResource(R.drawable.video_un);
          ib4.setImageResource(R.drawable.anli_un);
          ib5.setImageResource(R.drawable.me_un);
      }

      private void selectTab(int i){
             switch(i){
                 case 0:
                     ib1.setImageResource(R.drawable.article_pressed);
                     break;
                 case 1:
                     ib2.setImageResource(R.drawable.tware);
                     break;
                 case 2:
                     ib3.setImageResource(R.drawable.video);
                     break;
                 case 3:
                     ib4.setImageResource(R.drawable.anli);
                     break;
                 case 4:
                     ib5.setImageResource(R.drawable.me);
                     break;
             }
             vp.setCurrentItem(i);
      }

    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);
        return true;
    }

}
